import 'package:flutter/material.dart';
import 'package:flutter_yn_goods/config/app_translations.dart';
import 'package:flutter_yn_goods/utils/loading/loading_utils.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';


import '../../config/app_color.dart';

/// 项目中通用确认按钮
class CommonButton extends StatelessWidget {
  final String title;
  final Function()? onClick;
  final Future Function()? onAsyncClick;
  final double? height;
  final double? width;
  final double? fontSize;
  final FontWeight? fontWeight;
  final Color? textColor;
  final double horizontalPadding;
  final double verticalPadding;
  final bool enable;
  final Color? backgroundColor;
  final Color? disableTextColor;
  final Color? disableBackgroundColor;
  final bool alwaysCanClick;
  final MaterialStateProperty<OutlinedBorder?>? shape;
  final BorderSide? border;
  final BorderSide? disableBorder;
  final bool showBorder;
  final Duration? delayed;
  final String? statisticsAlias;
  final Color? shadowColor;

  // 最小宽度
  CommonButton({
    super.key,
    required this.title,
    this.onClick,
    this.enable = true,
    this.height,
    this.width,
    this.fontSize,
    this.fontWeight = FontWeight.w600,
    this.textColor,
    this.horizontalPadding = 0.0,
    this.verticalPadding = 0.0,
    this.backgroundColor,
    this.disableTextColor,
    this.disableBackgroundColor,
    this.alwaysCanClick = false,
    this.showBorder = false,
    this.shape,
    this.border,
    this.disableBorder,
    this.onAsyncClick,
    this.delayed = const Duration(milliseconds: 250),
    this.statisticsAlias,
    this.shadowColor,
  });

  // 最大宽度
  CommonButton.expend({
    super.key,
    required this.title,
    this.onClick,
    this.enable = true,
    this.height,
    this.width = double.infinity,
    this.fontSize,
    this.fontWeight = FontWeight.w600,
    this.textColor,
    this.horizontalPadding = 0.0,
    this.verticalPadding = 0.0,
    this.backgroundColor,
    this.disableTextColor,
    this.disableBackgroundColor,
    this.alwaysCanClick = false,
    this.showBorder = false,
    this.shape,
    this.border,
    this.disableBorder,
    this.onAsyncClick,
    this.delayed = const Duration(milliseconds: 250),
    this.statisticsAlias,
    this.shadowColor,
  });

  bool _isTapped = false;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: height ?? 44.0.w,
      width: width,
      child: ElevatedButton(
          onPressed: alwaysCanClick || enable ? _onTap : () {},
          style: ButtonStyle(
            backgroundColor: MaterialStateProperty.resolveWith<Color>((states) {
              if (states.contains(MaterialState.disabled)) {
                return disableBackgroundColor ??
                    Colors.transparent; //AppColor.hex_202020
              }
              return enable
                  ? (backgroundColor ?? AppColor.primary)
                  : (disableBackgroundColor ?? AppColor.color202020);
            }),
            foregroundColor: MaterialStateProperty.resolveWith<Color>((states) {
              if (states.contains(MaterialState.disabled)) {
                return disableTextColor ?? Colors.white.withOpacity(0.6);
              }
              return enable
                  ? (textColor ?? AppColor.color1B1B1B)
                  : (disableTextColor ?? Colors.white.withOpacity(0.6));
            }),
            shadowColor: MaterialStateProperty.resolveWith<Color>((states) {
              return shadowColor ?? Colors.transparent;
            }),
            shape: shape ??
                MaterialStateProperty.all<RoundedRectangleBorder>(
                  RoundedRectangleBorder(
                    borderRadius:
                        BorderRadius.circular((height ?? 52.w) / 2), // 设置按钮的圆角
                  ),
                ),
            padding: MaterialStateProperty.all<EdgeInsetsGeometry>(
              EdgeInsets.symmetric(
                horizontal: horizontalPadding,
                vertical: verticalPadding,
              ),
            ),

            // overlayColor: MaterialStateProperty.all(Colors.transparent),
            // surfaceTintColor: MaterialStateProperty.all(Colors.transparent),
            // splashFactory: NoSplash.splashFactory,
            // elevation: MaterialStateProperty.all(0),
            side: MaterialStateProperty.all(showBorder
                ? enable == false
                    ? disableBorder
                    : border
                : null),
          ),
          child: Text(
            title,
            style: TextStyle(
              color: enable
                  ? (textColor ?? Colors.white)
                  : (disableTextColor ?? Colors.white.withOpacity(0.6)),
              fontSize: fontSize ?? 14.sp,
              fontWeight: fontWeight ?? FontWeight.w600,
              height: 1.1,
              leadingDistribution: TextLeadingDistribution.even,
            ),
          )),
    );
  }

  _onTap() {
    FocusManager.instance.primaryFocus?.unfocus();
    if (!_isTapped) {
      _isTapped = true;
      if (onClick != null) {
        _tapWithOnTap();
      } else if (onAsyncClick != null) {
        _tapWithAsyncTap();
      }
    } else {
      if (!LoadingUtils.isShow) {
        LoadingUtils.showToast(LocaleKeys.too_many_operations.tr);
      }
    }
  }

  void _tapWithAsyncTap() async {
    await onAsyncClick?.call();
    _isTapped = false;
  }

  void _tapWithOnTap() {
    onClick?.call();
    if (delayed == null) {
      _isTapped = false;
    } else {
      Future.delayed(delayed!, () {
        _isTapped = false;
      });
    }
  }
}
